/*
 * Android NewsFeed Application is a project for the Technological Educational Institute of Athens.
 * It's purpose is to familiarise the Android SDK and development procedures to students.
 * 
 * 
 * Android NewsFeed version 0.00
 * 
 * $LastChangedDate: 2011-08-11 14:48:40 +0300 (Thu, 11 Aug 2011) $
 * $Author: snoopaki $
 * $Rev: 40 $
 */
package gr.teiath.newsfeed;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;
import android.util.Log;


public class Feeder {
	/** Array Lists */
    public ArrayList<String> titles = new ArrayList<String>();
    public ArrayList<String> links = new ArrayList<String>();
    public ArrayList<String> dates = new ArrayList<String>();
    
    public HashMap<String, String> contentMap = new HashMap<String, String>();
    
    private Document doc = null;
    //private TextNode node = null;
    
    public int feedId = -1;
    private static final String[] ILLEGAL_EXT = 
    	{".doc", "docx", ".pdf", ".jpg", "jpeg", ".gif", ".bmp", ".png", ".xls", ".ppt"};
    
    private static final String tag = "Feeder";
    
    // TODO Read all parse_type class names from DB
    private static final String[][] PARSE_TYPE_LIST = {
    	new String[]{
    		"extra_notices",
    		"extra_notices_date"	
    		},
    	new String[]{"main_page_notices"},
    	new String[]{
    		"content_title", // PARSE_LIST[2][0]
    		"content_datecreated_left", // PARSE_LIST[2][1]
        	}
    };
    
    private static final String[][] PARSE_TYPE_CONTENT = {
    	new String[]{
			"page_title", // PARSE_CONTENT[0][0]
			"content_datecreated", // PARSE_CONTENT[0][1]
			"content_text" // PARSE_CONTENT[0][2]
            },
    	new String[]{
			"page_title", // PARSE_CONTENT[1][0]
			"content_datecreated", // PARSE_CONTENT[1][1]
			"content_text" // PARSE_CONTENT[1][2]
        },
    	new String[]{
			"page_title", // PARSE_CONTENT[2][0]
			"content_datecreated", // PARSE_CONTENT[2][1]
			"content_text" // PARSE_CONTENT[2][2]
        }
    };

    private int parse_type;

   
	public Feeder(String parseURL, int p_type) {
	    try {                  
            /** Open URL with Jsoup */
	    	String ext = parseURL.substring(parseURL.length() - 4);
	    	if(!Arrays.asList(ILLEGAL_EXT).contains(ext)) {
	    		this.parse_type = p_type;
	    		this.doc = Jsoup.connect(parseURL).get();
	    	}	
        } catch (Exception e) {
            Log.e(tag, "****** Parse Exception on Jsoup.connect: " + e.getMessage());
            e.printStackTrace();
        }
	}
	
	public void getFeed() {
		if (doc != null) {
			/** Grab classes we want */   
	        Elements pcontents = doc.getElementsByClass(PARSE_TYPE_LIST[this.parse_type][0]);
	        

	        /**TODO Will not work if more parse_type 0 departments join up. 
	         * Must read class names from database and create specific loops for each parse_type 0!
	         */
	        //Loop for grabbing each content
	        for (Element content : pcontents) {
	        	Elements children = content.children(); // Content's children
	        	
	        	//Loop for grabbing each child
	        	for(Element child : children) {
		        	
	        		if(child.tag().toString().equals("a")) {
		        		links.add(child.attr("abs:href")); // parse absolute address  
		        		if(child.hasText()) 
		        			titles.add(child.text());
		        	} else {
		        		if(child.hasText()) {
		        			String temp = getDate(child.text()) != null ? getDate(child.text()) : null;
		        			if(temp != null) dates.add(temp); 
		        		}
		        	}
	        	}
	        }
	        
	        if(this.parse_type == 0) { // Get dates for parse type 0
	        	Elements pdates = doc.getElementsByClass(PARSE_TYPE_LIST[this.parse_type][1]);
	        	for (Element pdate : pdates) {
					dates.add(getDate(pdate.text())) ;
	        	}
	        } else if(this.parse_type == 2) { // Get dates for parse type 2
	        	Elements pdates = doc.getElementsByClass(PARSE_TYPE_LIST[this.parse_type][1]);
	        	for (Element pdate : pdates) {
					dates.add(getDate(pdate.text())) ;
	        	}
	        }
		}
	}
	
	public void getContent() {
		if (doc != null) {
	        /** Grab classes we want */                   
	        Elements ptitle = doc.getElementsByClass(PARSE_TYPE_CONTENT[this.parse_type][0]);
	        Elements pdate = doc.getElementsByClass(PARSE_TYPE_CONTENT[this.parse_type][1]);
	        Elements pcontent = doc.getElementsByClass(PARSE_TYPE_CONTENT[this.parse_type][2]);
	        
	        /** Loop for grabbing DATES within parent element */
	        for (Element date : pdate) {
	        	contentMap.put("date", date.text().toString());
	        }       
	        
	        /** Loop for grabbing TITLE within parent element */
	        for (Element title : ptitle) {
	        	contentMap.put("title", title.text().toString());
	        }
	                   
	        /** Loop for grabbing CONTENT within parent element */
	        for (Element content : pcontent) {          	
	        	//original
	        	//contentMap.put("content", content.text().toString());
	        	contentMap.put("content",  Jsoup.clean(content.html(), Whitelist.relaxed()));
	        }
		}
	}
		
	private static String getDate(String date) {
		Pattern datePattern = Pattern.compile("(\\d{2})[- /.](\\d{2})[- /.](\\d{4})");
	    Matcher dateMatcher = datePattern.matcher(date);
	    if (dateMatcher.find())
	    { 

	    	// Format date to yyyy-MM-dd for DB insert
	    	date = dateMatcher.group(3) + "-" + dateMatcher.group(2) + "-" + dateMatcher.group(1);
	    	return date;
	    } else {
	    	return null;
	    }
	 
	}

}
